//
//  SelfItemCell.m
//  JiaoX
//
//  Created by 尹桥印 on 14/11/16.
//  Copyright (c) 2014年 北京娇羞科技有限公司. All rights reserved.
//

#import "SelfItemCell.h"
#import "SelfItemInfo.h"
#import "SelfArrowItemInfo.h"
#import "SelfSwitchItemInfo.h"
#import "ZJSwitch.h"

#define BASE_INSET 45//基础偏移量，就是textLabel的x值

@interface SelfItemCell()
{
    UIImageView *_arrow;
    ZJSwitch *_switch;
    UILabel *_label;
    UIImageView *_newImage;
}
@end


@implementation SelfItemCell

+ (instancetype)itemCellWithTableView:(UITableView *)tableView{
    // 0.用static修饰的局部变量，只会初始化一次
    static NSString *ID = @"Cell";
    
    // 1.拿到一个标识先去缓存池中查找对应的Cell
    SelfItemCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 2.如果缓存池中没有，才需要传入一个标识创建新的Cell
    if (cell == nil) {
        cell = [[SelfItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 1.设置背景
        [self setupBg];
        
        // 2.设置子控件属性
        [self setupSubviews];
        
        [self addLabel];
        
        [self addNewPic];
    }
    return self;
}

- (void)setItem:(SelfItemInfo *)item
{
    _item = item;
    
    // 设置数据
    self.imageView.image = [UIImage imageNamed:item.icon];
    self.textLabel.text = item.title;
    self.textLabel.textColor = JXColor(102, 102, 102);
    
    if ([item isKindOfClass:[SelfArrowItemInfo class]]) {
        self.textLabel.textAlignment = NSTextAlignmentLeft;
        [self settingArrowWithItem:item];
    } else if ([item isKindOfClass:[SelfSwitchItemInfo class]]) {
        self.textLabel.textAlignment = NSTextAlignmentLeft;
        [self settingSwitchWithItem:item];
    } else {
        if(item.icon==nil){
            self.textLabel.textAlignment = NSTextAlignmentCenter;
        }else{
            self.textLabel.textAlignment = NSTextAlignmentLeft;
        }
        // 什么也没有，清空右边显示的view
        self.accessoryView = nil;
        // 用默认的选中样式
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
    }
    
    [self setLabelFrameWithTitle:item.title subTitle:item.subtitle];
    [self setNewPicFrameWithTitle:item.title isShowNew:item.hasNew];
}
- (void)addLabel{
    UILabel *label = [[UILabel alloc]init];
    label.textAlignment = NSTextAlignmentRight;
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = JXColor(153, 153, 153);
    _label = label;
    [self addSubview:label];
}

- (void)addNewPic{
    UIImageView *newImage = [[UIImageView alloc]init];
    newImage.image = [UIImage imageNamed:@"new"];
    [newImage setContentMode:UIViewContentModeScaleAspectFill];
    _newImage = newImage;
    //设置跟图片一样大小
    _newImage.frame = (CGRect){{0,0},newImage.image.size};
    _newImage.hidden = YES;
    [self addSubview:_newImage];
    
}

- (void)setLabelFrameWithTitle:(NSString *)title subTitle:(NSObject *)subTitle{
    
    if(subTitle !=nil){
        CGSize size = [title sizeWithFont:self.textLabel.font];
        //label的x值为title的x加title的width加2个publicinset;
        CGFloat labelX = size.width+BASE_INSET+JXPublicInset*2;
        CGFloat labelY = 0;
        //宽度等于整个屏幕的宽度-15(asscssoryView距左边的宽度)-asscssoryView.width-jxpublicinset-labelX
        //不知道为什么在这个地方cell的宽度是320，所能不能使用cell的宽度
        CGFloat labelW = JXScreenW-15-self.accessoryView.width-JXPublicInset/2-labelX;
        CGFloat labelH = self.height;
        _label.frame = CGRectMake(labelX, labelY, labelW, labelH);
        if ([subTitle isKindOfClass:[NSString class]]) {
            _label.text = (NSString *)subTitle;
        }else if([subTitle isKindOfClass:[NSAttributedString class]]){
            _label.attributedText = (NSAttributedString *)subTitle;
        }
        _label.hidden = NO;
    }else{
        _label.hidden = YES;
    }
}

-(void)setNewPicFrameWithTitle:(NSString *)title isShowNew:(BOOL)isShowNew{
    if(isShowNew){
        CGSize size = [title sizeWithFont:self.textLabel.font];
        //label的x值为title的x加title的width加2个publicinset;
        _newImage.x = size.width+BASE_INSET+JXPublicInset/2;
        _newImage.y = (self.height-_newImage.height)/2;
        _newImage.hidden = NO;
    }else{
        _newImage.hidden = YES;
    }
}

#pragma mark 设置右边的开关
- (void)settingSwitchWithItem:(SelfItemInfo *)item
{
    if (_switch == nil) {
        _switch = [[ZJSwitch alloc] initWithFrame:CGRectMake(100, 180, 60, 25)];
        _switch.onTintColor = JXThemeColor;
        _switch.onText = @"开";
        _switch.offText = @"关";
        [_switch addTarget:self action:@selector(switchChange) forControlEvents:UIControlEventValueChanged];
    }
    
    // 设置开关的状态
    SelfSwitchItemInfo *switchItem = (SelfSwitchItemInfo *)_item;
    [_switch removeTarget:self action:@selector(switchChange) forControlEvents:UIControlEventValueChanged];
    _switch.on = switchItem.on;
    [_switch addTarget:self action:@selector(switchChange) forControlEvents:UIControlEventValueChanged];
    
    // 右边显示开关
    self.accessoryView = _switch;
    
    // 禁止选中
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

#pragma mark 设置右边的箭头
- (void)settingArrowWithItem:(SelfItemInfo *)item
{
    if (_arrow == nil) {
        _arrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"self_item_arrow"]];
    }
    
    // 右边显示箭头
    self.accessoryView = _arrow;
    
    
    // 用默认的选中样式
    self.selectionStyle = UITableViewCellSelectionStyleDefault;
}

#pragma mark 开关状态改变
- (void)switchChange
{
    SelfSwitchItemInfo *switchItem = (SelfSwitchItemInfo *)_item;
    switchItem.on = _switch.on;
    if (switchItem.operation!=nil) {
        switchItem.operation();
    }
}

#pragma mark 设置子控件属性
- (void)setupSubviews
{
    self.textLabel.backgroundColor = [UIColor clearColor];
    self.textLabel.font = [UIFont systemFontOfSize:16];
    
    self.detailTextLabel.backgroundColor = [UIColor clearColor];
    self.detailTextLabel.font = [UIFont systemFontOfSize:12];
}

#pragma mark 设置背景
- (void)setupBg
{
    // 1.默认
    UIView *bg = [[UIView alloc] init];
    bg.backgroundColor = [UIColor whiteColor];
    self.backgroundView = bg;
    
    // 2.选中
    UIView *selectedBg = [[UIView alloc] init];
    selectedBg.backgroundColor = JXColor(237, 233, 218);
    self.selectedBackgroundView = selectedBg;
}

@end
